<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.Book" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null || !"admin".equals(user.getRole())) {
        response.sendRedirect("../login.jsp");
        return;
    }

    Book book = (Book) request.getAttribute("book");
    if (book == null) {
        response.sendRedirect("books?message=book_not_found");
        return;
    }
%>
<html>
<head>
    <title>编辑图书 - 图书管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        input[type="text"], input[type="number"], input[type="date"], textarea, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .btn-group {
            display: flex;
            gap: 10px;
            margin-top: 30px;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }
        .btn-primary {
            background: #007bff;
            color: white;
        }
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        .btn:hover {
            opacity: 0.9;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            border-bottom: 1px solid #eee;
            padding-bottom: 15px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>📚 编辑图书信息</h1>
        <a href="books" class="btn btn-secondary">返回图书列表</a>
    </div>

    <form action="books" method="post">
        <input type="hidden" name="action" value="update">
        <input type="hidden" name="bookId" value="<%= book.getBookId() %>">

        <div class="form-group">
            <label>ISBN *</label>
            <input type="text" name="isbn" value="<%= book.getIsbn() != null ? book.getIsbn() : "" %>" required>
        </div>

        <div class="form-group">
            <label>书名 *</label>
            <input type="text" name="title" value="<%= book.getTitle() != null ? book.getTitle() : "" %>" required>
        </div>

        <div class="form-group">
            <label>作者 *</label>
            <input type="text" name="author" value="<%= book.getAuthor() != null ? book.getAuthor() : "" %>" required>
        </div>

        <div class="form-group">
            <label>出版社</label>
            <input type="text" name="publisher" value="<%= book.getPublisher() != null ? book.getPublisher() : "" %>">
        </div>

        <div class="form-group">
            <label>出版日期</label>
            <input type="date" name="publishDate" value="<%= book.getPublishDate() != null ? book.getPublishDate() : "" %>">
        </div>

        <div class="form-group">
            <label>价格</label>
            <input type="number" name="price" step="0.01" value="<%= book.getPrice() != null ? book.getPrice() : "" %>">
        </div>

        <div class="form-group">
            <label>分类</label>
            <input type="text" name="category" value="<%= book.getCategory() != null ? book.getCategory() : "" %>">
        </div>

        <div class="form-group">
            <label>描述</label>
            <textarea name="description"><%= book.getDescription() != null ? book.getDescription() : "" %></textarea>
        </div>

        <div class="form-group">
            <label>总数量 *</label>
            <input type="number" name="totalCopies" value="<%= book.getTotalCopies() != null ? book.getTotalCopies() : 1 %>" min="1" required>
        </div>

        <div class="form-group">
            <label>可借数量 *</label>
            <input type="number" name="availableCopies" value="<%= book.getAvailableCopies() != null ? book.getAvailableCopies() : 1 %>" min="0" required>
        </div>

        <div class="form-group">
            <label>位置</label>
            <input type="text" name="location" value="<%= book.getLocation() != null ? book.getLocation() : "" %>">
        </div>

        <div class="form-group">
            <label>状态</label>
            <select name="status">
                <option value="available" <%= "available".equals(book.getStatus()) ? "selected" : "" %>>可借</option>
                <option value="borrowed" <%= "borrowed".equals(book.getStatus()) ? "selected" : "" %>>已借出</option>
            </select>
        </div>

        <div class="btn-group">
            <button type="submit" class="btn btn-primary">更新图书</button>
            <a href="books" class="btn btn-secondary">取消</a>
        </div>
    </form>
</div>
</body>
</html>